package pl.tssoft.math.core.calculation.objects;

import pl.tssoft.math.core.Value;
import pl.tssoft.math.core.Workspace;
import pl.tssoft.math.core.calculation.Calculable;
import pl.tssoft.math.core.exceptions.CalculationException;
import pl.tssoft.math.core.exceptions.ErrorCode;

/**
 *
 * @author tserafin
 */
public class VariableObject implements Calculable {

    private final String name;
    private final int startPosition;

    public VariableObject(String name, int startPosition) {
        this.name = name;
        this.startPosition = startPosition;
    }

    public Value calulate(Workspace w) throws CalculationException {
        Value v = w.getVariable(name);
        if (v == null) {
            throw new CalculationException(startPosition, ErrorCode.ERR_VARIABLE_NOT_FOUND, name);
        }
        return v;
    }
}
